﻿/// <reference path="../Libriries/jquery-2.0.3.js" />
/// <reference path="../Libriries/q.js" />
/// <reference path="class.js" />

var Captcha = (function () {

    var CAPTCHA_TEXT_STYLE = {
        'color': 'white',
        'display': 'inline-block',
        'font-size': '22px',
        'padding': '2px',
        'padding-right': '10px'
    };

    var CAPTCHA_BOX_STYLE = {
        'width': '100px',
        'height': '26px',
        'vertical-align': 'top',
        'font-size': '18px',
        'padding': '2px'
    };
    
    var firstNumber;
    var secondNumber;
    var expectedResult;

    var generateNumbers = function () {
        firstNumber = Math.floor(Math.random() * 10);
        secondNumber = Math.floor(Math.random() * 10);

        expectedResult = firstNumber + secondNumber;
    }

    var createText = function () {
        generateNumbers();

        var text = $("<span id='captcha-text'></span>");
        text.text(firstNumber + " + " + secondNumber + " = ");
        text.css(CAPTCHA_TEXT_STYLE);

        return text;
    }

    var createBox = function () {
        var box = $("<input type='text' id='captcha-result-box'/>");
        box.css(CAPTCHA_BOX_STYLE);

        return box;
    }

    var makeCaptcha = function (selector) {
        var holder = $(selector);

        holder.append(createText());
        holder.append(createBox());
    }

    var validate = function() {
        if (parseInt($("#captcha-result-box").val()) == expectedResult) {
            return true;
        }
        else {
            return false;
        }
    }

    return {
        makeCaptcha: makeCaptcha,
        validate: validate
    }
}());